//
// Created by dongbo01 on 3/13/19.
//
#include "p_paxos/pnode.h"
#include "p_paxos/role_node.h"
#include <unistd.h>


struct test_ppaxos : public ppax_cb {

    virtual void master_ischange() {
        T_LOGW("master_ischange  master_ischange  master_ischange :" << id);
    }

    int id;
};


int main(int argc, char const *argv[]) {

    int id = std::atoi(argv[1]);
    int port = std::atoi(argv[2]);

    char logprefix[256];
    sprintf(logprefix, "paxos%d", id);
    db_CLogThread::InitLogger(logprefix, true);

    struct evpaxos_config *conf = new (struct evpaxos_config );
    conf->proposers_count = 1;
    conf->proposers[0].addr = strdup("127.0.0.1");
    conf->proposers[0].port = port + 1;
    conf->proposers[1].addr = strdup("127.0.0.1");
    conf->proposers[1].port = port + 2;
    conf->proposers[2].addr = strdup("127.0.0.1");
    conf->proposers[2].port = port + 3;

    conf->acceptors_count = 1;
    conf->acceptors[0].addr = strdup("127.0.0.1");
    conf->acceptors[0].port = port + 1 + 1000;
    conf->acceptors[1].addr = strdup("127.0.0.1");
    conf->acceptors[1].port = port + 2 + 1000;
    conf->acceptors[2].addr = strdup("127.0.0.1");
    conf->acceptors[2].port = port + 3 + 1000;

    test_ppaxos ppaxos;
    ppaxos.id = id;

    auto p1 = new pnode(id, conf, nullptr, &ppaxos);
//    auto p2 = new pnode(1, conf);
//    auto p3 = new pnode(2, conf);

    p1->init_pnode();
//    p2->init_pnode();
//    p3->init_pnode();

    if (id == 0) {
        while (!p1->learner->is_ok())
            std::this_thread::sleep_for(std::chrono::seconds(1));

        int i = 0;
        for (; i < 10000000; ++i) {
            std::ostringstream ss;
            ss << "xxxxxxxxxxxxxxxxxxxxxxxxxxxx " << i;
            p1->client_node->SendMsg(ss.str(), i);

            std::this_thread::sleep_for(std::chrono::milliseconds(5));
        }
    }

    

    while (1)
        sleep(11);

    return 0;
}
